<?php
namespace app\admin\model;
use think\Db;
use think\Model;
/*聊天信息模型*/
class ChatMsg extends CommonModel
{

    public function __construct($args = [])
    {
        parent::__construct($args);
    }

    /** 获取10条消息记录
     * @param $fuser 请求历史记录的加密的client_id
     * @param $tuser 对方对话的加密的client_id
     * @return int
     */
    public function getlog($fuser,$tuser)
    {
        $sql = "
            SELECT mu.*,c.content,c.add_time,c.content_type, u.nickname, (select nickname from ecs_kf  where kf_id = u.kf_id ) as kf_nickname from ecs_chat_msg_user AS mu
            LEFT JOIN ecs_chat_msg AS c ON mu.msg_id = c.id
            LEFT JOIN ecs_user AS u ON u.enc_client_id = mu.fuser
            WHERE  ((mu.fuser = ? and mu.tuser = ?) or (mu.fuser = ? and mu.tuser = ?))
            ORDER BY c.id ASC
        ";

        $data =   Db::query($sql,[$fuser,$tuser,$tuser,$fuser]);
        if(!empty($data))
        foreach($data as $k=>$v)
        {
            if($v['content_type'] == 'photo' && strpos($v['content'],"https://") === false && strpos($v['content'],"http://") === false)
            {
                $data[$k]['content'] = $this->getDomain().'/'.$v['content'];
            }
            $data[$k]['time'] = $data[$k]['add_time'] = date('Y-m-d H:i:s',$v['add_time']);
            $data[$k]['nickname'] = !empty($v['kf_nickname']) ? $v['kf_nickname'] : $v['nickname'];
        }
        return $data;

    }

    public function getDomain()
    {
        $configModel = new Config();
        $config = $configModel->info(['cKey'=>'domain']);
        return !empty($config) ? $config['cValue'] : "";
    }


    public function getLogUser($client_id)
    {
        $log = db('chat_msg_user')->field('fuser,tuser')
            ->where('fuser|tuser','=',$client_id)
            ->order("id DESC")
            ->select();
        $userArr = [];
        foreach($log as $v)
        {
            if($v['fuser'] != $client_id)
                $userArr[] = $v['fuser'];
            if($v['tuser'] != $client_id)
                $userArr[] = $v['tuser'];
        }
        $userArr = array_unique($userArr);

        if(empty($userArr)) return [];

        foreach($userArr as $k=>$v)
        {
            $userArr[$k] = "'".$v."'";
        }
        //获取客户端id，匿名，以及当前用户与最近聊天的用户的相关没查阅的信息数量
        $sql = "
                select u.enc_client_id,u.nickname,u.kf_id,k.nickname as kf_nickname,
                  (select count(*) from ecs_chat_msg_user where tu_skim = 0 and tuser = '$client_id' and fuser = u.enc_client_id ) as no_skim
                  from ecs_user as u
                  LEFT JOIN ecs_kf as k ON k.kf_id = u.kf_id
                  where u.enc_client_id in (".implode($userArr,',').")
                  ORDER BY  FIND_IN_SET(u.enc_client_id,'".str_replace("'","",implode($userArr,','))."')";

        $data = Db::query($sql);
        if(!empty($data))
        {
            foreach($data as $k=>$v)
            {
                //nickname，如果是客服，则用客服匿名，如果nickname为空，则默认为'匿名'
                $data[$k]['nickname'] = empty($v['kf_nickname']) ? $v['nickname'] : (empty($v['nickname']) ? "匿名" : $v['nickname']);
            }
        }
        return $data;
    }

}